package com.gem.service.impl;

import com.gem.domain.Score;
import com.gem.domain.ScoreAPI;
import com.gem.domain.ScoreResult;
import com.gem.mapper.ScoreMapper;
import com.gem.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
@Transactional
public class ScoreServiceImpl implements ScoreService {

    @Autowired
    ScoreMapper scoreMapper;

    @Override
    public List<Score> showScore(Integer classId) {
        try {
            return scoreMapper.showScore(classId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Score> searchScore(Integer cId,String sname) {
        try {
            sname = "%" + sname + "%";
            return scoreMapper.searchScore(cId,sname);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean add(Integer  api) {
        try {
            return scoreMapper.add(api);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean update(ScoreAPI api) {
        try {
            return scoreMapper.update(api);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateMy(List<ScoreResult> lists) {
        try {
            boolean flag = true;
            for (ScoreResult list : lists) {
                if(list != null){
                    if (!scoreMapper.updateMy(list)){
                        flag = false;
                    }
                }
            }
            if (flag){
                return true;
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }
}
